package nancal.mp.web.me

import nancal.mp.db.mongo.entity.log.WithdrawalLog
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.Require
import nbcp.base.comm.JsonResult
import nbcp.base.comm.ListResult
import nbcp.base.comm.PagerModel
import nbcp.base.extend.HasValue
import nbcp.mvc.HttpContext
import nbcp.mvc.sys.ClientIp
import nbcp.myoql.db.mongo.query
import nbcp.myoql.db.mongo.queryById
import nbcp.myoql.db.mongo.update
import nbcp.myoql.weixin.wx
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.ClassPathResource
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate
import java.time.LocalDateTime
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("/cashLog")
class Cash_Controller {

    //查询所有CashLog  带分页
    @PostMapping("/list")
    fun cashLogList(
            @RequestBody query: PagerModel<Any?>,
            nickName: String,
            start: LocalDate?,
            end: LocalDate?
    ): ListResult<WithdrawalLog> {

        return mor.log.withdrawalLog.query()
                .whereIf(start != null && end != null) {
                    it.createAt mongoUntil (start!! to end!!.plusDays(1))
                }
                .whereIf(nickName.isNotEmpty()) {
                    it.user.name mongoEquals nickName
                }
                .limit(query.skip, query.take)
                .orderByDesc { it.createAt }
                .toListResult();
    }

    @Value("\${app.wx.mchSecret:}")
    var mchSecret: String = ""
//    @Autowired
//    private val payService : Wx2PayPersonService? = null

    //去转账   使用微信直接转账
    @PostMapping("/doTransCash")
    fun doTransCash(
            @Require id: String,
            desc: String,
            session: HttpServletRequest
    ): JsonResult {

        var desc = desc
        val cashLog = mor.log.withdrawalLog.queryById(id).toEntity()!!

        if (desc.isEmpty()) {
            desc = "合伙人提现"
        }

        val payRes = wx.pay.corpPayToUser(HttpContext.request.ClientIp, cashLog.amount, cashLog.user.openid, cashLog.code, desc)
                .payToPerson(mchSecret, ClassPathResource("wx-mch-cert/apiclient_cert.p12").inputStream)

        if (payRes.msg.HasValue) {
            return JsonResult.error(payRes.msg)
        }

        mor.log.withdrawalLog.update()
                .where { it.id mongoEquals id }
                .set { it.done to true }
                .set { it.createAt to LocalDateTime.now() }
                .exec()

        return JsonResult()
    }


    @PostMapping("/remark")
    fun cashRemark(
            @Require id: String,
            remark: String
    ): JsonResult {

        mor.log.withdrawalLog.update()
                .where { it.id mongoEquals id }
                .set { it.remark to remark }
                .exec()

        return JsonResult()
    }


}
